package TOOLS.uploader_YAMAHA_AOI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class readUploadDataService4YAMAHA {
	c_diplomat d=null;
	public readUploadDataService4YAMAHA(c_diplomat d) {
		this.d=d;
	}

	protected List<uploadResultDto> getUploadResults(String iData, int layer) {
		String [] rows=iData.split("Test:,");
		List<uploadResultDto> result=null;
		Map<String,uploadResultDto> snMap=new HashMap<>();
		for(String row:rows) {
//			Test:,98000596_T_05_001_01,03954230252302607V1,03954230252302607V1-1,06/12/2025 16:43:32,Y41305,NoData,OK,6602043000,C1,316.417,42.952,¶ͭ,1,1,temp,\\192.168.100.175\RepairStation\RSData\Judge\RS@98000596_T_05_001_01@03954230252302607V1@1@A@NG@20250612164305Image\RS@98000596_T_05_001_01@03954230252302607V1@1@A@20250612164305@P@31@C.jpg
			String [] para=row.split(",");
			if(para!=null && para.length>2) {
				String sn=para[1];
				String state=para[6].toUpperCase();
				String errMaterial=null;
				String errComp=null;
				String errText=null;
				String pos=null;
				if( para.length>12) {
					errMaterial=para[7];
					errComp=para[8];
					errText=para[11];
					pos=para[12];
				}
				snMap=curlResultState(snMap,sn,state,errMaterial,errComp,errText,pos,layer);
				snMap=curlResultData(snMap,sn,state,errMaterial,errComp,errText,pos,layer);
			}
		}
		result = new ArrayList<>(snMap.values());
		return result;
	}

	private Map<String, uploadResultDto> curlResultData(Map<String, uploadResultDto> snMap, String sn, String state,
			String errMaterial, String errComp, String errText, String pos, int layer) {
		uploadResultDto result=snMap.get(sn);
		if(result==null) {
			return null;
		}
		if(errText!=null) {
			if(result.datas==null) {
				result.datas=new ArrayList<>();
			}
			uploadDataDto iData=new uploadDataDto();
			iData.pos=pos;
			iData.name=errComp;
			iData.value=errText;
			iData.unit=errMaterial;
			iData.result=state;
			result.datas.add(iData);
			snMap.put(sn, result);
		}
		return snMap;
	}

	private Map<String, uploadResultDto> curlResultState(Map<String, uploadResultDto> snMap, 
			String sn, String state,String errMaterial, String errComp, String errText, String pos, int layer) {
		uploadResultDto result=snMap.get(sn);
		if(result==null) {
			result=new uploadResultDto();
			result.sn=sn;
			result.state=new uploadStateDto();
			result.state.serialNumber=sn;
			result.state.layer=layer;
			result.state.posState=new HashMap<>();
		}
		if(state.toUpperCase().equals("PASS")) {
			if(pos!=null) {
				result.state.posState.put(Integer.parseInt(pos), 0);
			}
			if(result.state.state==null) {
				result.state.state=0;
			}
		}else if(state.toUpperCase().equals("OK")) {
			if(pos!=null) {
				result.state.posState.put(Integer.parseInt(pos), 0);
			}
			if(result.state.state==null) {
				result.state.state=0;
			}
			result.state.repairType=1;
		}else if(state.toUpperCase().equals("NG")) {
			if(pos!=null) {
				result.state.posState.put(Integer.parseInt(pos), 1);
			}
			if(result.state.state==null || result.state.state!=1) {
				result.state.state=1;
			}
		}
		snMap.put(sn, result);
		return snMap;
	}
	
}
